import Taro from '@tarojs/taro';
import Service from './service';

export default class http extends Service {
    constructor() {
        super();
    }

    get({ url = '', params = {}, config = {} }, loading) {
        return new Promise((resolve, reject) => {
            this.fly
                .get(url, { params, ...config })
                .then(res => {
                    resolve(res);
                })
                .catch(error => {
                    reject(error);
                })
                .finally(() => {
                    if (loading) {
                        this.closeLoading();
                    }
                });
        });
    }

    post({ url = '', params = {}, config = {} }, loading) {
        return new Promise((resolve, reject) => {
            this.fly
                .post(url, params, config)
                .then(res => {
                    resolve(res);
                })
                .catch(error => {
                    reject(error);
                })
                .finally(() => {
                    if (loading) {
                        this.closeLoading();
                    }
                });
        });
    }

    closeloading() {
        setTimeout(() => {
            Taro.hideLoading();
        }, 1000);
    }

    loading({ text = '加载中...', show = false } = {}) {
        if (show) {
            Taro.showLoading({
                mask: true,
                title: text
            });
        }
    }
}
